package com.saodisheng.service.impl;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.saodisheng.model.LoginLog;
import com.saodisheng.service.AccountService;
import com.saodisheng.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description: 管理员用户首页业务逻辑
 *
 * @author 扫地生_saodisheng
 * @date 2022/1/29
 */
@Service
public class AdminLogServiceImpl implements AdminLogService {
	@Autowired
	private AccountService accountService;

	final LoginLog logDao = new LoginLog().dao();
	int pageSize = 15;
	
	@Override
	public Page<LoginLog> getLoginLog(int pageNumber) {
		String select = "select * ";
		String from = "from login_log order by id desc";
		
		Page<LoginLog> logPage = logDao.paginate(pageNumber, pageSize, select, from);
		
		accountService.join("accountId", logPage.getList(), "nickName");
		
		return logPage;
	}
	
	@Override
	public Ret delLog(int id) {
		boolean r = Db.deleteById("login_log", id);
		if (r) {
			return Ret.ok("msg", "删除成功");
		}
		return Ret.fail("msg", "删除失败");
	}
	
	@Override
	public Ret delAllLog() {
		int r = Db.update("truncate TABLE login_log");
		
		//执行成功返回0，失败返回-1
		if (r == 0) {
			return Ret.ok("msg", "删除成功");
		}
		return Ret.fail("msg", "删除失败");
	}
}
